<?php

include __DIR__.'/vendor/autoload.php';

/**
 *
 */
class oauth
{
    private $appid;
    private $appsecret;
    private $client;

    public function __construct(string $appid, string $appsecret)
    {
        $this->appid = $appid;
        $this->appsecret = $appsecret;
        $this->client = new \GuzzleHttp\Client(['base_uri'=>'https://openapi.w7.cc']);
    }

    /**
	 * "https://passport.w7.cc/oauth/authorize/index?appid=309250&redirect_url=http%3A%2F%2Fbaidu.com&install_type=0"
     * @return string
     */
    public function redirect(string $redirectUrl)
    {
        return $this->getLoginUrl($redirectUrl);
    }


    /**
	 * array(6) {
	'username' =>
	string(6) "memeto"
	'avatar' =>
	string(95) "https://cdn.w7.cc/images/avatar/0/410/884.jpg?v=1658830310&imageView2/5/w/100/h/100/format/webp"
	'nickname' =>
	string(6) "memeto"
	'founder_openid' =>
	string(22) "tFfxsAMp7YdTKINFQR46zw"
	'openid' =>
	string(22) "tFfxsAMp7YdTKINFQR46zw"
	'open_id' =>
	string(22) "tFfxsAMp7YdTKINFQR46zw"
	}
     * @param string $code
     * @return false|array
     * @throws OAuthException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function user(string $code)
    {
        $tokenArr = $this->getAccessToken($code);
		if (!is_array($tokenArr)) {
			return false;
		}
		$token = isset($tokenArr['access_token']) ? $tokenArr['access_token'] : false;
        if ($token === false) {
            return false;
        }
        $userInfo = $this->post('/we7/open/oauth/user/info', ['access_token' => $token]);

        return $userInfo;

    }

    /**
     * @param string $code
     * @return array | false
     */
    public function getAccessToken(string $code)
    {
        $result = $this->post('/we7/open/oauth/access-token/code', ['code'=>$code], true);
        return $result;
    }

    /**
     * @return array|bool
     */
    protected function getLoginUrl(string $redirectUrl)
    {
        $result = $this->post('/we7/open/oauth/login-url/index',
            [
                'appid'=>$this->appid,
                'redirect' => $redirectUrl,
            ], true);
        if (is_array($result))
        {
            return isset($result['url']) ? $result['url'] : false;
        }
        return false;

    }

    /**
     * 发送请求
     * @param string $url
     * @param array $data
     * @param bool $sign
     * @return false|string
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    protected function post(string $url, array $data, bool $sign = true)
    {
        if ($sign)
        {
            $data = $this->sign($data);
        }

        $result = $this->client->post($url, ['form_params' => $data]);
        if ($result->getStatusCode() === 200)
        {
            return json_decode($result->getBody()->getContents(), true);
        }
        return false;
    }

    private function sign(array $data) : array
    {

        $params = [
            'appid' => $this->appid,
            'timestamp' => time(),
            'nonce' => random_int(1000, 10000).'',
//            'code' => $code
        ];
        $params = array_merge($params, $data);

        ksort($params);
        reset($params);
        $params = array_map(function ($value){
            return is_null($value) ? '' : $value;
        }, $params);
        $sign = md5(http_build_query($params). $this->appsecret);
        $params['sign'] = $sign;
        return $params;
    }

}

$appid = '309250';
$appsecret = '';
$code = 'uIx56I9ZTx0EV6u5yE0UI6X9NU19EQeq';
$oauth = new oauth($appid, $appsecret);
$info = $oauth->user($code);
var_dump($info);
$url = $oauth->redirect('http://baidu.com');
var_dump($url);
